import contextlib


@contextlib.contextmanager
def temp_setattr(ob, attr, new_value):
    """Temporarily set an attribute on an object for the duration of the
    context manager.

    Does not take into account whether the attribute was found through
    inheritance or off the object directly.

    """
    sentinel = object()
    old_value = getattr(ob, attr, sentinel)
    setattr(ob, attr, new_value)
    yield
    if old_value is sentinel:
        delattr(ob, attr)
    else:
        setattr(ob, attr, old_value)



@contextlib.contextmanager
def temp_setattr(ob, attr, new_value):
    """Temporarily set an attribute on an object for the duration of the
    context manager."""
    replaced = False
    old_value = None
    if hasattr(ob, attr):
        try:
            if attr in ob.__dict__:
                replaced = True
        except AttributeError:
            if attr in ob.__slots__:
                replaced = True
        if replaced:
            old_value = getattr(ob, attr)
    setattr(ob, attr, new_value)
    yield replaced, old_value
    if not replaced:
        delattr(ob, attr)
    else:
        setattr(ob, attr, old_value)
